<?php

namespace app\admin\controller;

use app\admin\model\SystemRole;
use think\Controller;
use think\Db;
use app\admin\model\SystemManager;
use app\admin\model\UserVideo as user_video_info;

/**
 * 管理员
 */
class Manager extends Basic
{

    // 初始化
    public function initialize()
    {
        parent::_initialize();

    }

    /*
     * 显示
     */
    public function index()
    {
        $SystemManager = new SystemManager();
        $system_role = new SystemRole();
        $token = Input('post.token');
        $type = Input('type', 1);
        $page = Input('page', 1);
        $limit = Input('limit', 10);
        if (!$token) {
            json_fail(data('-5', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $list = $SystemManager->index($type, $page, $limit);
            if ($list) {
                foreach ($list as $k=>$v){
                    $v['role_name'] =$system_role->where(array('id'=>$v['role_id']))->value('name');
                    $v['register_time'] = $v['register_time']? date('Y-m-d H:i:s',$v['register_time']):'--';
                    $list[$k]=$v;
                }
                $infoList = array(
                    'list'=>$list,
                    'count'=>$SystemManager->where('status','neq', '0')->order('id desc')->count(1)
                );
                json_success($infoList);
            } else {
                $infoList = array(
                    'list'=>[],
                    'count'=>0
                );
                json_success($infoList);
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }


    /**
     * 添加
     */
    public function add()
    {
        //  MDAwMDAwMDAwMIGHhWuycm9x
        //   print_t(encode(1560));
        //实例化数据库
        $SystemManager = new SystemManager();
        $data = input('post.');
        $username = input('post.username');
        $password = input('post.password');
        $identity = input('post.identity');
        $token = input('post.token');
        if (!$token) {
            json_fail(data('-1', 'token不能为空!'));
        }
        if (!$username) {
            json_fail(data('-2', '用户名不能为空!'));
        }
        if (!$password) {
            json_fail(data('-5', '密码不能为空!'));
        }
        if (!$identity) {
            json_fail(data('-3', '身份不能为空!'));
        }

        $status = $SystemManager->isTrue($data);
        if ($status) {
            json_fail(data('-5', '用户名已存在!'));
        }
        $data['password'] = encryption($data['password']);
        if ($data['token'] == encryption(config('token'))) {
            $data['register_time'] = time();
            $data['role_id'] = $data['identity'];
            $row = $SystemManager->allowField(true)->save($data);
            if (!$row) {
                json_fail(data('-1', '添加失败'));
            } else {
                json_success(data('1', '添加成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

    /*
     * 修改
     */
    public function edit()
    {
        //实例化数据库
        $SystemManager = new SystemManager();
        $data = input('post.');
        $username = input('post.username');
        $password = input('post.password');
        $identity = input('post.identity');
        $token = input('post.token');
        $id = input('post.id');
        if (!$id) {
            json_fail(data('-2', 'ID不能为空!'));
        }
        if (!$token) {
            json_fail(data('-1', 'token不能为空!'));
        }
        if (!$username) {
            json_fail(data('-2', '用户名不能为空!'));
        }
        if (!$password) {
            json_fail(data('-5', '密码不能为空!'));
        }
        if (!$identity) {
            json_fail(data('-3', '身份不能为空!'));
        }

        $data['password'] = encryption($password);
        if ($data['token'] == encryption(config('token'))) {
            $data['role_id'] = $data['identity'];
            $row = $SystemManager->allowField(true)->save($data, ['id' => $id]);
            if ($row) {
                //添加关联表
                json_success(data('1', '修改成功'));
            }
        } else {
            json_fail(data('-8', 'token验证错误!'));
        }
    }


    /*
 * 删除
 */
    public function del()
    {

        $SystemManager = new SystemManager();
        //实例化数据库
        $id = input('post.id');
        $token = input('post.token');
        if (!$id) {
            json_fail(data('-2', 'ID不能为空!'));
        }
        if (!$token) {
            json_fail(data('-1', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $data['status'] = 0;
            $id = $SystemManager->where('id', $id)->update($data);
            if (!$id) {
                json_fail(data('-1', '删除失败'));
            } else {
                json_success(data('1', '删除成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

    //修改员工显示内容
    public function show_edit(){
        $data = Input('post.');
        $id = Input('post.id');
        if (!isset($data['token'])) {
            json_fail(data('-5', 'token不能为空!'));
        }
        if (!$id) {
            json_fail(data('-1', '获取id错误!'));
        }
        if ($data['token'] == encryption(config('token'))){
            if (!empty($data)){
                $info = Db('SystemManager')->where('id',$id)->field('id,role_id,username,truename,password,mobile,identity')->find();
                if (!$info){
                    json_success([]);
                }else{
                    json_success($info);
                }
            }else{
                json_fail(data('-2', '数据不能为空'));
            }
        }else{
            json_fail(data('-3', 'token验证错误!'));
        }
    }
    /**
     * 修改密码
     */
    public function edit_pass_word()
    {
        $data = input('post.');
        if (!isset($data['id'])) {
            json_fail(data('-2', 'ID不能为空!'));
        }
        if (!$data['id']) {
            json_fail(data('-2', 'ID不能为空!'));
        }

        if (!isset($data['old_password'])) {
            json_fail(data('-3', '原密码不能为空!'));
        }
        if (!$data['old_password']) {
            json_fail(data('-3', '原密码不能为空!'));
        }
        //实例化数据库
        $systemManager = new SystemManager();
        //判断原密码是否一致
        $data['old_password'] = encryption($data['old_password']);
        $status = $systemManager->isTruePass($data);
        if ($status == 2) {
            json_fail(data('-4', '原登录密码输入不正确!'));
        }
        //判断两次密码是否一致
        if (!isset($data['newPassword'])) {
            json_fail(data('-9', '新密码不能为空!'));
        }
        if (!$data['newPassword']) {
            json_fail(data('-9', '新密码不能为空!'));
        }
        if (!isset($data['newConfirmPassword'])) {
            json_fail(data('-10', '确认密码不能为空!'));
        }
        if (!$data['newConfirmPassword']) {
            json_fail(data('-10', '确认密码不能为空!'));
        }
        $newPassword = encryption($data['newPassword']);   //加密新密码
        $newConfirmPassword = encryption($data['newConfirmPassword']);    //加密确认密码
        if ($newPassword !== $newConfirmPassword) {
            json_fail(data('-5', '新密码与确认密码不一致!'));
        }
        //解密
        $data['id'] = decode($data['id']);
        if (!empty($data)) {
            $data['password'] = $newPassword;
            $id = $systemManager->edit($data);
            if (!$id) {
                json_fail(data('-6', '修改失败'));
            } else {
                json_success(data('1', '修改成功'));
            }
        } else {
            json_fail(data('-7', '数据不能为空'));
        }
    }

}